package com.qyt.hotel.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qyt.hotel.entity.Guest;
import com.qyt.hotel.entity.ResideState;
import com.qyt.hotel.entity.Result;
import com.qyt.hotel.entity.StatusCode;
import com.qyt.hotel.service.GuestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/guest")
@Slf4j
public class GuestController {

    @Resource
    private GuestService guestService;

    @GetMapping("/list")
    public Result getGuestList(@RequestParam("pageIndex") Integer pageIndex, @RequestParam("pageSize") Integer pageSize, @RequestParam("guestName") String guestName, @RequestParam("resideStateId") Integer resideStateId) {
        PageHelper.startPage(pageIndex, pageSize);
        List<Guest> guestList = guestService.getGuestList(guestName, resideStateId);
        // 封装查出的信息,并设置连续显示的页数
        PageInfo pageInfo = new PageInfo(guestList, 5);
        return new Result("查询成功", true, StatusCode.OK, pageInfo);
    }

    @GetMapping("/resideStateList")
    public Result getResideStateList() {
        List<ResideState> resideStateList = guestService.getResideStateList();
        if (!resideStateList.isEmpty()) {
            return new Result("查询成功", true, StatusCode.OK, resideStateList);
        } else {
            return new Result("查询失败", false, StatusCode.ERROR, null);
        }
    }

    @GetMapping("/roomIdList")
    public Result getRoomIdList(@RequestParam("roomTypeId") Integer roomTypeId, @RequestParam("roomStateId") Integer roomStateId) {
        List<String> roomIdList = guestService.getRoomIdList(roomTypeId, roomStateId);
        System.out.println(roomIdList);
        if (!roomIdList.isEmpty()) {
            return new Result("查询成功", true, StatusCode.OK, roomIdList);
        } else {
            return new Result("查询失败", false, StatusCode.ERROR, null);
        }
    }

    @PostMapping("/add")
    public Result addGuest(@RequestParam("guestName") String guestName, @RequestParam("guestNum") Integer guestNum, @RequestParam("identityId") String identityId, @RequestParam("phone") String phone, @RequestParam("resideDate") String resideDate, @RequestParam("roomTypeId") Integer roomTypeId, @RequestParam("roomId") String roomId, @RequestParam("deposit") Double deposit) {
        Integer count = guestService.addGuest(guestName, guestNum, identityId, phone, resideDate, roomTypeId, roomId, deposit);
        if (count != null) {
            // 根据房间号修改房间状态
            Integer count2 = guestService.updateRoomState(roomId);
            if (count2 != null) {
                return new Result("添加成功", true, StatusCode.OK, null);
            } else {
                return new Result("添加失败", false, StatusCode.ERROR, null);
            }
        } else {
            return new Result("添加失败", false, StatusCode.ERROR, null);
        }

    }

    @GetMapping("/getOne")
    public Result getOne(@RequestParam("guestId") Integer guestId) {
        Guest guest = guestService.getOne(guestId);
        if (null != guest) {
            return new Result("查询成功", true, StatusCode.OK, guest);
        } else {
            return new Result("查询失败", false, StatusCode.ERROR, null);
        }

    }

    @PostMapping("/update")
    public Result updateGuest(@RequestParam("guestId") Integer guestId, @RequestParam("guestName") String guestName, @RequestParam("guestNum") Integer guestNum, @RequestParam("identityId") String identityId, @RequestParam("phone") String phone, @RequestParam("resideDate") String resideDate, @RequestParam("roomTypeId") Integer roomTypeId, @RequestParam("roomId") String roomId, @RequestParam("oldRoomId") String oldRoomId, @RequestParam("deposit") Double deposit) {
        Integer count = guestService.updateGuest(guestId, guestName, guestNum, identityId, phone, resideDate, roomTypeId, roomId, deposit);
        // 根据房间号修改房间状态为入住
        Integer count2 = guestService.updateRoomState(roomId);
        // 修改客户入住房间的状态为空闲
        Integer count3 = guestService.updateOldRoomState(oldRoomId);
        return new Result("修改成功", true, StatusCode.OK, null);
    }

    @PostMapping("/delete")
    public Result deleteGuest(@RequestParam("guestId") Integer guestId) {
        guestService.deleteGuest(guestId);
        return new Result("修改成功", true, StatusCode.OK, null);
    }

    @PostMapping("/checkout")
    public Result checkout(@RequestParam("guestId") Integer guestId) {
        Guest guest = guestService.getOne(guestId);
        if (guest != null) {
            String resideDate = guest.getResideDate();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
            SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date = new Date();
            // 需要保存的离开日期
            String leaveDate = sdf2.format(date);
            // 用于计算天数的日期
            String leaveDate2 = sdf1.format(date);
            int day = 0;
            try {
                Date date1 = sdf1.parse(resideDate);
                Date date2 = sdf1.parse(leaveDate2);
                day = (int) ((date2.getTime() - date1.getTime()) / (3600 * 1000 * 24));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Double price = guest.getRoom().getRoomType().getRoomTypePrice();
            Double totalMoney = day * price;
            System.out.println(totalMoney);
            guestService.checkout(guestId, totalMoney, leaveDate);
            // 将房间修改为空闲状态
            guestService.updateRoomStateFree(guest.getRoomId());
            return new Result("结账成功", true, StatusCode.OK, totalMoney);
        } else {
            return new Result("结账失败", false, StatusCode.ERROR, null);
        }
    }
}
